package cfca.paperless.client.servlet._9_unite;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;

import cfca.paperless.client.bean.FieldBean;
import cfca.paperless.client.bean.ImageBean;
import cfca.paperless.client.bean.MultiDataBean;
import cfca.paperless.client.bean.PdfBean;
import cfca.paperless.client.bean.SealUserInfo;
import cfca.paperless.client.bean.SignLocation;
import cfca.paperless.client.bean.TextBean;
import cfca.paperless.client.servlet.PaperlessConfig;
import cfca.paperless.client.servlet.PaperlessUniteClient;
import cfca.paperless.client.util.Base64;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PaperlessUtil;
import cfca.paperless.client.util.StrategyUtil;

/**
 * @Description 联合接口(合成业务模板,合成业务数据,自动化签章)
 * @author clsu
 * @CodeReviewer TODO
 */
public class PaperlessUniteTest04 {

    public static void main(String[] args) throws Exception {
        try {
            long timeStart = System.currentTimeMillis();// 开始时间
            
            PaperlessUniteClient clientBean = new PaperlessUniteClient(PaperlessConfig.uniteUrl, 10000, 60000);

            //////////////////////////////////合成模板(synthesizeAutoTemplate)///////////////////////////////////
            String templateCode = "tmp005";
            String fieldBeanListXml = PaperlessUtil.buildFieldBeanListXml(getFieldBeanList());
            String textBeanListXml = PaperlessUtil.buildTextBeanListXml(getTextBeanList());
            String imageBeanListXml = PaperlessUtil.buildImageBeanListXml(getImageBeanList());
            clientBean.addSynthesizeAutoTemplateParams(templateCode, fieldBeanListXml, textBeanListXml, imageBeanListXml);
            
            
            ///////////////////////////如下为用户1手写签名相关的数据，如果没用用户1此处可以省略/////////////////////////////////////////////////
            byte[] handwritingImage1 = FileUtils.readFileToByteArray(new File("./TestData/handwriting.png"));
            byte[] handwritingData1 = handwritingImage1;
            byte[] identificationFrontData = FileUtils.readFileToByteArray(new File("./TestData/1.png"));
            byte[] identificationBackData = FileUtils.readFileToByteArray(new File("./TestData/2.png"));
            List<MultiDataBean> list1 = new ArrayList<MultiDataBean>();
            list1.add(new MultiDataBean(handwritingData1, "handwritingData.dat", "media-id", "0"));//手写签名数据
            list1.add(new MultiDataBean(handwritingImage1, "handwritingImage.png", "2"));//手写签名图片
            list1.add(new MultiDataBean(identificationFrontData, "identificationFrontImage.png", "2"));//身份证正面
            list1.add(new MultiDataBean(identificationBackData, "identificationBackImage.png", "2"));//身份证反面
            String proofHashAndMultiData1[] = PaperlessUtil.getProofHashAndMultiData(list1);
            
            SignLocation signLocation = new SignLocation(1, "代理人签名", "R", 30, -7);
            SealUserInfo sealUserInfo = new SealUserInfo("张三a", "Z", "00000000000000000", "15744659823", "北京", "签合同");
            String proofSealStrategyXml = StrategyUtil.createProofSealStrategyXml(handwritingImage1, sealUserInfo, signLocation, proofHashAndMultiData1[0]);
            String sealLayer2Text = "宋江";
            String sealLayer2TextStyle = "font-size:12;";
            //文字章皮肤
            proofSealStrategyXml = StrategyUtil.createProofSealStrategyXml(sealLayer2Text,sealLayer2TextStyle, sealUserInfo, signLocation, proofHashAndMultiData1[0]);
            System.out.println(111);
            ///////////////////////////以上为用户1的数据/////////////////////////////////////////////////
            
            /////////////////////////////////蓋章(sealAutoPdf)////////////////////////////////////////
            String sealCode = "org001";//
            String sealPassword = "11111111";
            // 可以为空，不为空则此图片会替代此处印章的印模图片
            sealUserInfo = new SealUserInfo("张三", "beijing", "reason");
            // 3=关键字签章
            signLocation = new SignLocation(1, 100, 100);//new SignLocation("人签名", "R", 0, 0);
            // 生成签章策略文件
            String sealStrategyXml = StrategyUtil.createSealStrategyXml(sealCode, sealPassword, "", sealUserInfo, signLocation);
            ///////////////////////////////////////////////////////////////////////////////////////////
            
            String compoundSealStrategyXml = "<List>" + proofSealStrategyXml + sealStrategyXml +  "</List>";
            String bizTypeCode = "B001";//业务类型，比如：开户、开通网银
            PdfBean pdfBean = new PdfBean("", "a", bizTypeCode, "auto");//系统自动存储,存储的路径自动生成
            String multiData = "[" + proofHashAndMultiData1[1] + "]";
            String pdfBeanXml = PaperlessUtil.buildPdfBeanXml(pdfBean);
            clientBean.addCompoundSealAutoPdfParams(pdfBeanXml, multiData, compoundSealStrategyXml);
            
            /////////////////////////////////////////////////////////////////////////////////////////
            
            // 操作员编码或者机构号
            String operatorCode = PaperlessConfig.operatorCode;
            // 渠道编码，可以为空
            String channelCode = PaperlessConfig.channelCode;
            
            byte[] resultData = clientBean.uniteReqAndRes(operatorCode, channelCode);

            String errorRsString = PaperlessUtil.isError(resultData);

            // 处理结果为正常，保存签章后的PDF文件到本地目录下
            if ("".equals(errorRsString)) {
                System.out.println("OK:sealAutoPdf");
                System.out.println(resultData.length);
                IoUtil.write("./TestData/output/sealedPdf-unite.pdf", resultData);
            } else {
                // 处理结果为异常，打印出错误码和错误信息
                System.out.println("NG:sealAutoPdf");
                System.out.println(resultData.length);
                System.out.println(new String(resultData, "UTF-8"));
            }
            
            long timeEnd = System.currentTimeMillis();// 结束时间
            System.out.println("time:" + (timeEnd - timeStart));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    private static List<FieldBean> getFieldBeanList() throws Exception {
        List<FieldBean> fieldBeanList = new ArrayList<FieldBean>();
String address = "11的快捷付款捷付款的机发及啊啊啊啊啊啊啊啊啊啊";
        fieldBeanList.add(new FieldBean("operator", "会计法", "", "text"));
        fieldBeanList.add(new FieldBean("date", "2011年11月11", "", "text", 2));
        fieldBeanList.add(new FieldBean("branch", "ddddddddddddd", "", "text", 2));
        fieldBeanList.add(new FieldBean("fingerCode", "Sucaili", "", "text", 2));
        fieldBeanList.add(new FieldBean("signature1", "Sucaili", "", "text",1));
        fieldBeanList.add(new FieldBean("identNo", "Sucaili", "", "text",1));
        fieldBeanList.add(new FieldBean("identType", "Sucaili", "", "text"));
        fieldBeanList.add(new FieldBean("ICcardType", "Sucaili", "", "text",1));
        fieldBeanList.add(new FieldBean("accountName", "张凡的", "", "text",1));
        fieldBeanList.add(new FieldBean("cardNo1", "Sucaili", "", "text",1));
        fieldBeanList.add(new FieldBean("sequenceNo1", "Sucaili", "", "text",1));
        fieldBeanList.add(new FieldBean("address", address, "", "text",3));
        fieldBeanList.add(new FieldBean("B1", "Sucaili", "", "text",1));

        return fieldBeanList;
    }
    
    private static List<TextBean> getTextBeanList() throws Exception {
        List<TextBean> textBeanList = new ArrayList<TextBean>();
        TextBean textBean1 = new TextBean("你好啊\r\n我的啊\r\ndddddd", "", "银行签章：", "R", 0, 0);
        textBean1.setTextFontSize("15");
        textBean1.setTextColor("aa000000");
        TextBean textBean2 = new TextBean("545454545452212323\r\ndddddddddddddddddddddddd\r\n打开房间打开附件\r\ndkjfdkjf你的额尔", "", "单据号：", "R", 0, 0);
//        textBean2.setSpacingHeight(20);//设置行间距默认15像素
        textBean2.setTextFontSize("15");
        textBean2.setTextColor("aa000000");
        textBeanList.add(textBean1);
        textBeanList.add(textBean2);

        return textBeanList;
    }
    
    private static List<ImageBean> getImageBeanList() throws Exception {
        List<ImageBean> imageBeanList = new ArrayList<ImageBean>();
        // 如下代码是在pdf中添加可以显示的图片
        byte[] imageData = FileUtils.readFileToByteArray(new File("./TestData/1.png"));
        ImageBean imageBean0 = new ImageBean("100", "100", "签章", "C", 0, 0, imageData,1);
        String imagePath = Base64.toBase64String("/temp/2.png".getBytes("utf-8"));
        ImageBean imageBean = new ImageBean("100", "100", 1, "400", "300", imagePath,2);
        imageBean.setImagePath(imagePath);
        imageBeanList.add(imageBean0);
        imageBeanList.add(imageBean);
        return imageBeanList;
    }

}
